package sg.edu.nus.iss.club.gui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
import java.util.List;

import javax.swing.JOptionPane;

import sg.edu.nus.iss.club.BadBookingException;
import sg.edu.nus.iss.club.Booking;
import sg.edu.nus.iss.club.Club;
import sg.edu.nus.iss.club.Facility;
import sg.edu.nus.iss.club.Member;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ClubApplication {
	
	private ClubWindow clubWindow;
	private Club club;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		ClubApplication clubApp = new ClubApplication();
		clubApp.start();
	}
	
	public ClubApplication() {
		this.club = new Club();
	}
	
	public void start() {
		this.clubWindow = new ClubWindow();
		this.clubWindow.clubApplication = this;
		this.clubWindow.initGUI();
		this.clubWindow.addWindowListener(new CloseWindowListener());
		this.clubWindow.setVisible(true);
		this.clubWindow.setLocationRelativeTo(null);
	}
	
	public void addMember(String surname, String firstName, String secondName) {
		club.addMember(surname, firstName, secondName);
	}
	
	public void removeSelectedMember(int memberNumber) {
		club.removeMember(memberNumber);
	}
	
	public Member[] getMembers() {
		return club.getMembers().toArray(new Member[0]);
	}
	
	public void addFacility(String name, String description) {
		club.addFacility(name, description);
	}
	
	public void removeSelectedFacility(String name) {
		club.removeFacility(name);
	}
	
	public Facility[] getFacilities() {
		return club.getFacilities().toArray(new Facility[0]);
	}
	
	public void addBooking(Date startDate, Date endDate) {
		int memberNumber = -1;
		if (getMainWindow().getMemberPanel().getSelectedMember() != null) {
			memberNumber = getMainWindow().getMemberPanel().getSelectedMember().getMemberNumber();
		}		
		Facility facility = getMainWindow().getFacilityPanel().getSelectFacility();
		String facilityStr = "";
		if (facility != null) {
			facilityStr = getMainWindow().getFacilityPanel().getSelectFacility().getName();
		}
		try {
			club.addBooking(memberNumber,facilityStr, startDate, endDate);
		} catch (BadBookingException e) {
			JOptionPane.showMessageDialog(null, "Booking cannot be added due to invalid input", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void removeSelectedBooking(Booking booking) {
		club.removeBooking(booking);
	}
	
	public Booking[] getBookings(Date bookingDate) {
		Facility fac = getMainWindow().getFacilityPanel().getSelectFacility();
		List<Booking> bookingList = club.getBookings(fac, bookingDate);
		if (bookingList != null && bookingList.size() > 0) {
			return club.getBookings(fac, bookingDate).toArray(new Booking[0]);
		} else {
			return new Booking[0];
		}
	}
	
	private class CloseWindowListener extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			shutdown();
		}
	}
	
	public void shutdown() {
		this.clubWindow.dispose();
		System.exit(1);
	}
	
	public ClubWindow getMainWindow() {
		return this.clubWindow;
	}
	
	
}
